package com.jsaas.sys.controller;


import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jsaas.controller.base.BaseController;
import com.jsaas.core.qparams.QueryParamsUtils;
import com.jsaas.model.Menu;
import com.jsaas.sys.service.MenuService;

/**   
* @Title: MenuController.java 
* @Package com.jsaas.sys.controller 
* @Description: TODO(系统菜单) 
* @author tuozq 
* @date 2017年11月9日 下午2:50:05 
* @version V1.0   
*/
public class MenuController extends BaseController {
	
	public void index(){
		render("listMenu.html");
	}
	
	public void edit(){
		String id = getPara("id");
		if(StrKit.isBlank(id)){
			Menu menu = new Menu();
			menu.setIsLeaf(true);
			setAttr("menu", menu);
		}else{
			Menu menu = Menu.dao.findById(id);
			setAttr("menu", menu);
		}
		setAttr("menus", enhance(MenuService.class).getAllMenu());
		render("editMenu.html");
	}
	
	@Before(Tx.class)
	public void save(){
		Menu menu = getModel(Menu.class, "menu");
		Ret ret = enhance(MenuService.class).save(menu);
		renderJson(ret);
	}
	
	@Before(Tx.class)
	public void delete(){
		String id = getPara("id");
		renderJson(enhance(MenuService.class).delete(id));
	}
	
	/**
	 * 菜单树形数据
	 */
	public void tree(){
		renderJson(enhance(MenuService.class).getTree(QueryParamsUtils.getAllParams(getRequest())).get("data"));
	}
	
	public void query(){
		Page<Menu> page = enhance(MenuService.class).getList(initPageParams(getRequest(), "menu.findMenu"));
		renderJson(page);
	}
	
	
}
